﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Factory
{
    public class SpcSerasaDAO
    {
        public static IList<SpcSerasa> ReaderToObjects(IDataReader reader)
        {
            if (reader.IsClosed)
            {
                throw new ArgumentException("O DataReader fornecido já estava fechado.");
            }

            int Id = reader.GetOrdinal("Id");
            int cpf = reader.GetOrdinal("CPF");


            IList<SpcSerasa> result = new List<SpcSerasa>();

            while (reader.Read())
            {          
                SpcSerasa s = new SpcSerasa() 
                {
                    cpf = reader.GetString(cpf),
                    id=reader.GetInt32(Id),
                };
                result.Add(s);
            }
            return result;
        }

        

        public IList<SpcSerasa> ObterCheques()
        {
            try
            {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT Id
                    ,CPF
                    FROM SpcSerasa";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                SqlDataReader dr = cmd.ExecuteReader();

                IList<SpcSerasa> spcSerasa = ReaderToObjects(dr);

                return spcSerasa;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }
    }
}
